<template>
  <div>
    <el-card shadow="always" class="message-box">消息框</el-card>
  </div>
</template>
<script>
import { MsgHeader, MsgType, SmsReq } from "../../security_msg_pb";
const crypto = require("crypto");
const fs = require("fs");
export default {
  created() {
  },
  methods: {
    onSmsRead(data) {
      console.log("MessageInformation...");
      let flag = data.toString("utf8", 0, 4);
      if (flag !== "AB47") {
        return;
      }
      let headerSize = data.readInt32LE(4);
      let msgHeader = MsgHeader.deserializeBinary(
        data.subarray(8, headerSize + 8)
      );
      let msgType = msgHeader.getMsgType();
      let msgSize = msgHeader.getMsgSize();
      switch (msgType) {
        case MsgType.MSG_SEND_REQ:
          console.log("MSG_SEND_REQ");
          let cipherText = data.subarray(
            8 + headerSize,
            msgSize + 8 + headerSize
          );
          fs.readFile("data/.shadow/mine.pem", (err, prv_key) => {
            let text = crypto.privateDecrypt(
              {
                key: prv_key.toString(),
                padding: crypto.constants.RSA_PKCS1_PADDING
              },
              cipherText
            );
            let smsReq = SmsReq.deserializeBinary(text);
            console.log(smsReq.getText());
          });
          break;
        case MsgType.MSG_RECV_RESP:
          console.log("发送完成");
          break;
      }
    }
  }
};
</script>
<style scoped>
.message-box {
  height: 395px;
}
</style>